package org.lq.his.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.persistence.Index;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.SortingFocusTraversalPolicy;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.lq.his.dao.UserDao;
import org.lq.his.pojo.Role;
import org.lq.his.pojo.User;
import org.lq.his.service.PermissionService;
import org.lq.his.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Resource
	private PermissionService permissionService;

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	
	@RequestMapping("/main")
	public String main() throws Exception{
		
		return "main";
	}
	@RequestMapping("/index")
	public String Index()throws Exception{
		return "user/userIndex";
	}
	@RequestMapping("/userForm")
	public String userForm()throws Exception{
		return "user/userForm";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login()throws Exception{
		return "login";
	}
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String userCode, String password, HttpSession session)throws Exception{
		User user =userService.getByUserCode(userCode);
		if(user!=null) {
			String salt = user.getSalt();
			Md5Hash md5Hash = new Md5Hash(userCode,user.getSalt());
			String pwd = md5Hash.toString();
			
			System.out.println(pwd+","+user.getPassword());
			System.out.println(user);
			
			if(pwd.equals(user.getPassword())) {
				return "redirect:/user/main";
			}
		}
		session.setAttribute("error", "账号或密码错误");
		return "login";
	}
	@RequestMapping("/selectUserById")
	@ResponseBody
	public User selectUserById(Integer id)throws Exception{
		
		return userService.getUserById(id);
	}
	@RequestMapping("/editUser")
	@ResponseBody
	public Map<String, Object> editUser(@RequestParam("photo") MultipartFile photo,User user ,Integer[] roleIds,HttpServletRequest request)throws Exception{

		if(!photo.isEmpty()) {
			//保存文件到指定路径
			//获取服务器的绝对路径
			String realPath = request.getServletContext().getRealPath("/img/userImage");
			System.out.println(realPath);
			String oldName = photo.getOriginalFilename();
			String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
			File dest = new File(realPath+File.separator+newName);
			photo.transferTo(dest);
			user.setImage("img/userImage/"+newName);
		}
		userService.edtiUser(user, roleIds);
		Map<String, Object> map = new HashMap<>();
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/selectAll")
	@ResponseBody
	public List<User> selectAll()throws Exception{
		
		return userService.getAll();
		
	}
	
	@RequestMapping("/listByCondition")
	@ResponseBody
	public Map<String, Object> listByCondition(Integer page,Integer rows,@RequestParam(defaultValue="id")String sort,@RequestParam(defaultValue="asc")String order,User condition)throws Exception{
		
		Map<String, Object> map = new HashMap<>();
		
		Integer start = (page-1)*rows;
		List<User> list = userService.getListByCondition(start, rows, condition, sort, order);
		
		Integer total = userService.getCountByCondition(condition);
		map.put("rows", list);
		map.put("total", total);
		return map;
		
		
	}
	@RequestMapping("/addUser")
	@ResponseBody
	public Map<String, Object> addUser(@RequestParam("photo") MultipartFile photo,User user,Integer[] roleIds,HttpServletRequest request)throws Exception{
		

		if(!photo.isEmpty()) {
			//保存文件到指定路径
			//获取服务器的绝对路径
			String realPath = request.getServletContext().getRealPath("/img/userImage");
			System.out.println(realPath);
			String oldName = photo.getOriginalFilename();
			String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
			File dest = new File(realPath+File.separator+newName);
			photo.transferTo(dest);
			user.setImage("img/userImage/"+newName);
		}
		userService.addUser(user,roleIds);
		Map<String, Object> map = new HashMap<>();
		map.put("result", true);
		return map;
	}
	@RequestMapping("/deleteUser")
	@ResponseBody
	public Map<String, Object> bathDeleteUser(Integer[] ids)throws Exception{
		userService.bathDeleteUser(ids);
		Map<String, Object> map = new HashMap<>();
		map.put("result", true);
		return map;
	}
	@RequestMapping("/selectShiftDoctor")
	@ResponseBody
	public List<User> selectShiftDoctor(Integer registrationChargeTypeId, Integer sectionId)throws Exception{
		
		return userService.getShiftDoctorByCondition(registrationChargeTypeId, sectionId);
	}
	
}
